#pragma once
#ifndef GAMEPANEL_H
#define GAMEPANEL_H
#include "raylib.h"
#include "scene.h"
#include "scene_manager.h"
#include <button.h>
#include <iostream>
#include <tilemap.h>
#include "hero.h"
#include "monster.h"
#include <chunk.h>
#include <mapbase.h>
#include <search/dfs.h>
#include <vector>
#include <map>
#include <mutex>
#include <thread>
// #include "../src/scene_east.cpp"


class GameScene:public Scene
{
    private:
        SceneManager& sceneManager;
        std::unordered_map<std::string, Texture2D>& texture_blocks;
        Font* font;
        TileMap map;
        Monster monster;
    public :
        Hero &hero; 
        int nodeCount=0;
        bool pauseFlag;//用来标识是否游戏是否暂停
        bool ahead;
        bool canDrawJumpScare=false;
        bool canDrawJumpScareForLava=false;
        bool badEnd=false;
        int on_fire_count=0;

        clock_t fireTimer;
        clock_t jumpScareTimer;
        double gameSceneTimeInterval=0.0f;
        

        Sound smile;
        bool hasPlayed=false;
        void initSounds();

        bool Timer(float time);

        //事件标识
        bool run_on_grass=false;
        bool run_on_fire=false;
        bool run_in_monster=false;

        void init() override;
        GameScene(SceneManager& manager,std::unordered_map<std::string, Texture2D>& texture_blocks, Font* font,Hero &hero);
        ~GameScene();
        TextButton btn_open{1500,900,200,150,monster.chasing?"Chase":"Stop"};
        TextButton btn_next{1500,1100,200,150,"Next"};
        TextButton btn_pre{1500,1300,200,150,"Preview"};
        virtual void render(float deltaTime);
        vector<int> activeDirections;
        Vector2 getHeroPosition();

        bool isEnd();
        bool getEndPlace();
        
        vector<Texture2D> jumpScareTextures;
        void initPictures();

        //检测hero移动的线程部分
        std::thread moveThread;
        bool moveThreadRunning = false;
        void moveDetectionThread();

        //检测事件的线程
        std::thread detectThread;
        bool detectEventRunning = false;
        void detectEventThread();

        //monster的相关操作
        void initMonster();
        std::map<int,vector<Rectangle>> getMonsterNearbyBlocks();
        void refreshMonsterCurrentFrames(float deltaTime);
        void handleMonster();

        //操控移动的函数
        int evaluate_wall_to_hero(Rectangle wall);
        //自主移动的相关操作函数
        void increaseNode();
        void decreaseNode();
        void detect_hero_move();
        void refreshHeroCurrentFrames(float deltaTime);
        void initMap(string mapPath);
        void go_ahead();
        void go_back();
        void test();
        int goto_from_to(Vector2 start,Vector2 end);
        void hero_goto_next();
        void hero_goto_preview();

        //基于区块和距离的碰撞检测
        int blockSize=48*4;//4X4的区块
        std::map<Coord<int>,Chunk> heroChunks;
        std::map<Coord<int>,Chunk> monsterChunks;
        std::map<Coord<int>, std::vector<Rectangle>> gridWalls;//区块坐标+存储对应的方块数组
        std::map<Coord<int>, std::vector<Rectangle>> gridSeedBlocks;
        std::map<Coord<int>, std::vector<Rectangle>> gridFireBlocks;
        std::map<int,vector<Rectangle>> getBlocks();
        std::map<int,vector<Rectangle>> getNearbyBlocks();
        void initBlocks();

        void updateShowInfo(double gameSceneTimeInterval);
        void updateShowInfo();
        void handleDrawJumpScare();
        string heroPositon="hero("+std::to_string(hero.x)+","+std::to_string(hero.y)+")";
        string monsterPositon="monster("+std::to_string(monster.x)+","+std::to_string(monster.y)+")";
        
};

#endif